/*
 * FileName: DailyController.java
 *
 */
package com.mrc.ak.showMan.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mrc.ak.showMan.service.LikeTiemsService;
import com.mrc.ak.showMan.service.ShowManService;
import com.mrc.ak.showMan.utils.GetDirNameUtil;
import com.mrc.core.base.CommonController;
import com.mrc.core.base.Constant;
import com.mrc.core.model.SysUser;
import com.mrc.core.service.SysLogsService;
import com.mrc.core.service.SysUserService;

/**
 * <p>
 * Description:  随机照片Contorller
 * </p>
 *
 * @author Mrc
 * @version 4.1

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2017-1-7 下午4:50:38          Mrc        4.1         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Controller
@RequestMapping(value = "/showMan/showManController.do")
public class ShowManController extends CommonController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysLogsService sysLogsService;
    @Autowired
    private ShowManService showManService;
    @Autowired
    private LikeTiemsService likeTiemsService;

    private Logger logger = Logger.getLogger(ShowManController.class);

    /**
     * 转到查看页面
     * @param request
     * @return
     */
    @RequestMapping(params = "method=toShowPicPage")
    public ModelAndView toShowPicPage(HttpServletRequest request) {
        SysUser user = sysUserService.getCurrentUser(request);
        ModelAndView mv = new ModelAndView();
        mv.addObject("name", user.getName());
        if (user.getMobile().equals("1")) {
            mv.addObject("times", "999");
        } else {
            mv.addObject("times", showManService.getLastTimes(user.getMobile()));
        }
        mv.setViewName("showPic");
        return mv;
    }

    /**
     * 获得图片
     * @param name
     * @param request
     * @return
     * @throws IOException 
     */
    @ResponseBody
    @RequestMapping(params = "method=getPic")
    public Map<String, Object> getPic(@RequestParam("name") String name, HttpServletRequest request, HttpServletResponse response) throws IOException {
        SysUser user = sysUserService.getCurrentUser(request);
        //记录用户日志
        sysLogsService.save(Constant.LOGS_TYPE_SHOWMAN_SEARCH, user.getMobile(), "搜索查看的内容：" + name);
        logger.debug("搜索查看的内容：" + name);
        Map<String, Object> fileMap = showManService.getPic(name);
        return fileMap;
    }

    /**
     * 点赞   顶或者踩(isParise)
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(params = "method=likeIt")
    public void LikeIt(HttpServletRequest request) {
        SysUser user = sysUserService.getCurrentUser(request);
        //是否是点赞
        String parise = request.getParameter("isParise");
        //文件夹
        String folderName = request.getParameter("folderName");
        folderName = GetDirNameUtil.getDirName(folderName);//转换
        //文件
        String fileName = request.getParameter("fileName");

        if ("true".equals(parise)) {
            sysLogsService.save(Constant.LOGS_TYPE_SHOWMAN_LIKE, user.getMobile(), "顶的内容：" + folderName + "/" + fileName);//记录用户日志
            likeTiemsService.pariseIt(folderName, fileName);
        } else {
            sysLogsService.save(Constant.LOGS_TYPE_SHOWMAN_LIKE, user.getMobile(), "踩的内容：" + folderName + "/" + fileName);//记录用户日志
            likeTiemsService.treadIt(folderName, fileName);
        }
    }

}
